//
//  AddCommentViewController.m
//  UGuideUS
//
//  Created by Will Wang on 22/07/2012.
//  Copyright (c) 2012 UGuideUs. All rights reserved.
//

#import "AddCommentViewController.h"

@interface AddCommentViewController ()

@end

@implementation AddCommentViewController

@synthesize userName=_userName;
@synthesize locationName=_locationName;
@synthesize userNameContent=_userNameContent;
@synthesize locationNameContent=_locationNameContent;

@synthesize commentContent=_commentContent;
@synthesize AllTheComments=_AllTheComments;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nil bundle:nil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    NSLog(@"In ADDComment, userName=%@",_userNameContent);
    NSLog(@"In ADDComment, locationName=%@",_locationNameContent);
    _userName.text=_userNameContent;
    _locationName.text=_locationNameContent;
}

- (void)viewDidUnload
{
    [self setUserName:nil];
    [self setLocationName:nil];
    [self setCommentContent:nil];
    [self setCommentContent:nil];
    [self setAllTheComments:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)returnKeyboard : (id)sender {
    [sender resignFirstResponder];
}

- (IBAction)submitComment:(id)sender {
    if([_commentContent.text isEqualToString:@""]){
        _AllTheComments.text=@"Please input the comment";
    }else{
    //Get a new databassAccessor
    DatabaseAccessor *getComment=[[DatabaseAccessor alloc]init];
    [getComment setTheLocation:_locationNameContent];
    [getComment setUserName:_userNameContent];
    [getComment setCommentToBeInserted:_commentContent.text];
    
    //get the comments of this locations from database
    NSString *jsonresult=[getComment insertCommentWithUserName];
    
    //fiter other useless information by using distinguish ] signal
    NSArray *filter=[jsonresult componentsSeparatedByString:@"]"];
    NSString *jasonAfterFilter=[filter objectAtIndex:0];
    //reconstruct the string by adding ] into JSON format
    jasonAfterFilter=[jasonAfterFilter stringByAppendingString:@"]"];
    
    //   NSLog(@"Filter after recrived data:%@",jasonAfterFilter);
    
    NSArray *JASONResults = [jasonAfterFilter JSONValue];
    NSLog(@"JAON results:%@",JASONResults);
    
    NSString *resultToBePrinted=@"";
    for(NSDictionary *aComment in JASONResults){
        //        NSLog(@"username=%@;date=%@;\r\n comment:%@\r\n", [aComment objectForKey:@"username"], [aComment objectForKey:@"date"],[aComment objectForKey:@"comment"]);
        resultToBePrinted=[resultToBePrinted stringByAppendingString:[NSString stringWithFormat:@"Username: %@; \r\nDate: %@;\r\n%@\r\n\r\n", [aComment objectForKey:@"username"], [aComment objectForKey:@"date"],[aComment objectForKey:@"comment"]]];
    }
    _AllTheComments.text=resultToBePrinted;
        
    }
}

@end
